<?php
/** An interface to the Hosting Reborn API transaction functions
 *
 * @package com\hostingreborn\api
 * @link https//api.hostingreborn.com/docs/functions/transaction/
 *
 * @author      Jon Cram <jon@hostingreborn.com>
 * @copyright   https://api.hostingreborn.com/code/copyright/
 * @license     https://api.hostingreborn.com/code/license/
 * @version     1.0
 *
 * $Date$
 * $Revision$
 *
 * 
 */

require_once('HR_api.class.php');

class HR_transactionApi extends HR_api {
    
    /***************************************************************************/
    /* Attributes                                                              */
    /***************************************************************************/

    /***************************************************************************/
    /* Constructors                                                            */
    /***************************************************************************/

    /***************************************************************************/
    /* Public methods                                                          */
    /***************************************************************************/


    /**
     * @link http://api.hostingreborn.com/docs/functions/transactions/get/
     *
     * @param string $key
     * @param string $transaction
     * @return stdClass
     */
    public function get($key = NULL, $transaction = NULL) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
        */

        // Prepare request variables
        $queryString = http_build_query(array(
                'key' => $key,
                'transaction' => $transaction
        ));

        // Send request
        $transactionXML = $this->read('transactions/get/?'.$queryString)->toXML();

        // Parse response
        $transaction = new stdClass();
        $transactionDOM = new DOMDocument();
        $transactionDOM->loadXML($transactionXML);

        $xpath = new DOMXPath($transactionDOM);
        $query = '/transaction/*';

        $nodes = $xpath->query($query);

        foreach ($nodes as $index => $element) {
            $value = NULL;
            $expression = '/transaction/'.$element->tagName.'/text()';
            $xpathResult = $xpath->evaluate($expression);

            if ($xpathResult->length >= 1) {
                if ($xpathResult->item(0) instanceof DOMText) {
                    $value = $xpathResult->item(0)->wholeText;
                }
            }

            if (!($value === NULL)) {
                $key = $element->tagName;
                $transaction->$key = $value;
            }
        }

        return $transaction;
    }


    /**
     * @link http://api.hostingreborn.com/docs/functions/transactions/list/
     *
     * @param string $key
     * @param int $limit
     * @param string $from
     * @param string $to
     * @param bool $storage
     * @param bool $bandwidth
     * @param bool $buynow
     * @param bool $manualcredit
     * @param bool $subscriptionPayment
     * @param bool $subscriptionControl
     * @param string $subscriptionId
     * @param string $domain
     * @return string XML
     */
    public function getList($key = NULL, $limit = NULL, $from = NULL, $to = NULL, $storage = NULL, $bandwidth = NULL, $buynow = NULL, $manualcredit = NULL, $subscriptionPayment = NULL, $subscriptionControl = NULL, $subscriptionId = NULL, $domain = NULL) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
        */

        // Prepare request variables
        $queryString = http_build_query(array(
                'key' => $key,
                'limit' => $limit,
                'from' => $from,
                'to' => $to,
                'storage' => $storage,
                'bandwidth' => $bandwidth,
                'instant_credit_purchase' => $buynow,
                'manual_credit_addition' => $manualcredit,
                'subscription_purchase' => $subscriptionPayment,
                'subscription_control' => $subscriptionControl,
                'subscription_id' => $subscriptionId,
                'domain' => $domain
        ));

        // Send request
        return $this->read('transactions/list/?'.$queryString)->toXML();
    }

    /***************************************************************************/
    /* Private methods                                                         */
    /***************************************************************************/

}

?>